﻿using MediatR;
using System.Threading.Tasks.Dataflow;
using Traceability.App.Applications.Command.TraceabilityManagement;
using Traceability.App.Applications.Command.TraceabilityManagement.Dto;
using Traceability.Domain.Entity;
using Traceability.Domain.Repository;
using Traceability.Enum;

namespace Traceability.App.Applications.CommandHand.TraceabilityManagementmodel
{
    public class TraceabilityGetAllCommandHandler : IRequestHandler<TraceabilityGetAllCommand, ApiResult<ApiPaging<List<TraceabilityDto>>>>
    {
        private readonly IBaseRepository<TraceabilityManagement> _traceability;
        private readonly IBaseRepository<ProductName> _productname;
        private readonly IBaseRepository<AquacultureModel> _aquculture;
        private readonly IBaseRepository<DeepProcessingModel> _deepprocessingModel;
        private readonly IBaseRepository<EnterpriseModel> _EnterpriseModel;
        private readonly IBaseRepository<ImageModel> _image;
        private readonly IBaseRepository<SlaughterModel> _slaught;
        private readonly IBaseRepository<HealthModel> _health;


        public TraceabilityGetAllCommandHandler(IBaseRepository<TraceabilityManagement> traceability, IBaseRepository<ProductName> productname, IBaseRepository<AquacultureModel> aquculture, IBaseRepository<DeepProcessingModel> deepprocessingModel, IBaseRepository<EnterpriseModel> enterpriseModel, IBaseRepository<ImageModel> image, IBaseRepository<SlaughterModel> slaught, IBaseRepository<HealthModel> health)
        {
            _traceability = traceability;
            _productname = productname;
            _aquculture = aquculture;
            _deepprocessingModel = deepprocessingModel;
            _EnterpriseModel = enterpriseModel;
            _image = image;
            _slaught = slaught;
            _health = health;
        }

        public Task<ApiResult<ApiPaging<List<TraceabilityDto>>>> Handle(TraceabilityGetAllCommand request, CancellationToken cancellationToken)
        {
            var result = new ApiResult<ApiPaging<List<TraceabilityDto>>>();
            List<TraceabilityDto> listdto = new List<TraceabilityDto>();
            var Traceability = _traceability.GetAll().ToList();
            foreach (var item in Traceability)
            {

                listdto.Add(new()
                {
                    id = item.id,
                    ProductName=_productname.GetAll().FirstOrDefault(x=>x.id==item.ProductNameid)?.productName,
                    ProductNameid=item.ProductNameid,
                    Batch=item.Batch,
                    ShelfLife=item.ShelfLife,
                    ProductionLicense=item.ProductionLicense,
                    Manufacturer=item.Manufacturer,
                    Informationlmprovement= GetInformationlmprovement(item.Batch),
                    createUserName=item.createUserName,
                    createTime=item.createTime,
                    ProductInspectionReportPath=item.ProductInspectionReportPath,
                    updateUserName=item.createUserName,
                    CoverImage=item.CoverImage,
                    del=item.del,

                });


            };
            var list = listdto.Where(x => x.del == 0).ToList();
            if (request.produceid > 0)
            {

                list = list.Where(x => x.ProductNameid == request.produceid).ToList();
            }
            if (!string.IsNullOrEmpty(request.Batch))
            {

               list=list.Where(x=>x.Batch==request.Batch).ToList();
            }
            if (!string.IsNullOrEmpty(request.ProductionLicense))
            {

                list = list.Where(x => x.ProductionLicense == request.ProductionLicense).ToList();
            }
            if (!string.IsNullOrEmpty(request.Manufacturer))
            {

                list = list.Where(x => x.Manufacturer == request.Manufacturer).ToList();
            }
            if (request.ShelfLife >0)
            {

                list = list.Where(x => x.ShelfLife == request.ShelfLife).ToList();
            }

            var count = list.Count();
            var pcount = (int)Math.Ceiling(count * 1.0 / request.size);
            var pagelist = list.OrderBy(x => x.id).Skip((request.index - 1) * request.size).Take(request.size);

            var page = new ApiPaging<List<TraceabilityDto>>();
            page.totalcount = count;
            page.totalpcount = pcount;
            page.data=pagelist.ToList();

            result.data = page;

            return Task.FromResult(result);
        }

        public int GetInformationlmprovement(string batch)
        {
            var information = 0;
            if (_aquculture.GetAll().FirstOrDefault(x => x.Batch == batch) != null)
            {
                information += 10;
                
            }
            if (_health.GetAll().FirstOrDefault(x => x.Batch == batch) != null)
            {
                information += 10;
            }
            if (_EnterpriseModel.GetAll().FirstOrDefault(x => x.Batch == batch && x.StepsType == 1) != null)
            {
                information += 10;
            }
            if (_slaught.GetAll().FirstOrDefault(x => x.Batch == batch) != null)
            {
                information += 15;
            }
            if (_EnterpriseModel.GetAll().FirstOrDefault(x => x.Batch == batch && x.StepsType == 2) != null)
            {
                information += 15;
            }
            if (_deepprocessingModel.GetAll().FirstOrDefault(x => x.Batch == batch) != null)
            {
                information += 20;
            }
            if (_EnterpriseModel.GetAll().FirstOrDefault(x => x.Batch == batch && x.StepsType == 3) != null)
            {
                information += 20;
            }
            return information;

        }


    }
}
